//
//  CHSKVOViewController.m
//  ZXCategories_Example
//
//  Created by zhuxm2023 on 2024/1/5.
//  Copyright © 2024 海参. All rights reserved.
//

#import "CHSKVOViewController.h"

@interface CHSKVOViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, copy) NSArray *array;
@property (nonatomic, copy) NSString *price;

@end

@implementation CHSKVOViewController
@synthesize price = _price;

- (void)dealloc
{
    [self.tableView removeObserver:self forKeyPath:NSStringFromSelector(@selector(contentOffset))];
    NSLog(@"dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = self.navTitle;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self testKVC];
    [self testKVO];

}


- (void)testKVC{
    
    [self setValue:@"1" forKey:@"price"];
    NSLog(@"%@",[self valueForKey:@"price"]);
    
    NSDictionary *dic = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value1"};
    
    NSArray *objs = [dic objectsForKeys:@[@"key1"] notFoundMarker:@"1"];
    NSLog(@"[dic objectsForKeys:@[key1] notFoundMarker:1]:%@",objs);
    [self setValue:@[@"1",@"2"] forKey:@"array"];
    NSMutableArray *mutableArrayValue = [self mutableArrayValueForKey:@"array"];
    NSLog(@"%@",mutableArrayValue);
}

- (void)setPrice:(NSString *)price{
    if (_price != price) {
        _price = @"5";
    }
}

- (NSString *)price{
    return _price;
}


- (void)testKVO{
    [self.tableView addObserver:self forKeyPath:NSStringFromSelector(@selector(contentOffset)) options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)observeValueForKeyPath:(nullable NSString *)keyPath
                      ofObject:(nullable id)object
                        change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change
                       context:(nullable void *)context{
    if([keyPath isEqualToString:@"contentOffset"])
    {
        NSLog(@"%@",NSStringFromCGPoint(self.tableView.contentOffset));
    }
}



- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
